using System.Collections.Generic;
using System.Xml;

namespace ServiceModel.Syndication.LEAP2A {
    /// <summary>
    /// Represents an address in a LeapItem
    /// </summary>
    public class LeapSpatialAddress {
        private readonly List<string> addressLines = new List<string>();

        /// <summary>
        /// Gets or sets the lines of the address.
        /// </summary>
        /// <value>The address lines.</value>
        public List<string> AddressLine {
            get { return addressLines; }
        }

        /// <summary>
        /// Gets or sets the postcode.
        /// </summary>
        /// <value>The post code.</value>
        public string PostCode {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the country.
        /// </summary>
        /// <value>The country.</value>
        public LeapSpatialCountry Country {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapSpatialAddress"/> class.
        /// </summary>
        public LeapSpatialAddress() {
  }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapSpatialAddress"/> class.
        /// </summary>
        /// <param name="lines">The address lines.</param>
        /// <param name="postcode">The postcode.</param>
        public LeapSpatialAddress(IEnumerable<string> lines, string postcode) {
            AddressLine.AddRange(lines);
            PostCode = postcode;
        }

        /// <summary>
        /// Writes to a LEAP2A feed.
        /// </summary>
        /// <param name="writer">The writer.</param>
        public void WriteTo(XmlWriter writer, LeapFeed feed) {
            foreach (string line in AddressLine) {
                writer.WriteElementString(
                        feed.LeapPredicatesNamespacePrefix,
                        "addressline",
                        feed.LeapPredicatesNamespace,
                        line);
            }

            if (!string.IsNullOrEmpty(PostCode)) {
                writer.WriteElementString(
                        feed.LeapPredicatesNamespacePrefix,
                        "postcode",
                        feed.LeapPredicatesNamespace,
                        PostCode);
            }

            if (Country != null) {
                Country.WriteTo(writer, feed);
            }
        }

        /// <summary>
        /// Tries to parse the leap spatial element from an Atom feed.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public bool TryParseLeapSpatial(XmlReader reader) {
            while (reader.LocalName != "spatial") {
                if (reader.NodeType == XmlNodeType.Element) {
                    switch (reader.LocalName) {
                        case "addressline":
                            AddressLine.Add(reader.ReadString());
                            break;
                        case "postcode":
                            PostCode = reader.ReadString();
                            break;
                        case "country":
                            string code = reader.GetAttribute("countrycode", reader.NamespaceURI);
                            Country = new LeapSpatialCountry(code, reader.ReadString());
                            break;
                    }
                }

                if (!reader.Read()) break;
            }

            return true;
        }
    }
}
